package com.ruicar.afs.cloud.afscase.approvemakelabel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.approvemakelabel.entity.LoanLabelInfo;
import com.ruicar.afs.cloud.afscase.approvemakelabel.service.LoanLabelInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.afscase.manuallabel.condition.ManualLabelCondition;
import com.ruicar.afs.cloud.afscase.manuallabel.entity.ManualLabel;
import com.ruicar.afs.cloud.afscase.manuallabel.service.ManualLabelService;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xueyu.ma
 * @version 1.0
 * @description: 手动打标签
 * @created 2020/7/1
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api("放款标签信息保存")
@RequestMapping("/makeLoanLabel")
public class LoanLabelInfoController {

    private LoanLabelInfoService loanLabelInfoService;
    private ManualLabelService manualLabelService;
    private CaseContractInfoService caseContractInfoService;

    @PostMapping(value = "/addLoanLabelInfo/{params}")
    @ApiOperation(value = "新增信审标签信息")
    @Transactional
    @SysLog("新增信审标签信息")
    public IResponse<Boolean> addLoanLabelInfo(@PathVariable String params) {
        String[] str = params.split(",");
        String applyNo = str[0];
        //根据申请编号查询List 保留整体对象查询
        List<LoanLabelInfo> oldLabelList = loanLabelInfoService.queryLoanLabelByApplyNo(applyNo);
        List<String> idsList = new ArrayList<>();
        if (oldLabelList != null & oldLabelList.size() > 0) {
            for (int i = 0; i < oldLabelList.size(); i++) {
                idsList.add(oldLabelList.get(i).getId().toString());
            }
        }
        //根据ID批量删除
        if (idsList != null && idsList.size() > 0) {
            loanLabelInfoService.removeByIds(idsList);

        }
        //保存新增
        List<LoanLabelInfo> labelList = new ArrayList();
        String strIds = "";
        for (int i = 1; i < str.length; i++) {
            strIds += str[i] + ",";
            LoanLabelInfo loanLabelInfo = new LoanLabelInfo();
            ManualLabel manualLabel = manualLabelService.queryAllLabelListById(Long.parseLong(str[i]));
            loanLabelInfo.setApplyNo(applyNo);
            loanLabelInfo.setLabelId(manualLabel.getId());
            loanLabelInfo.setLabelName(manualLabel.getLabelName());
            loanLabelInfo.setLabelPhase(manualLabel.getLabelPhase());
            loanLabelInfo.setLabelColor(manualLabel.getLabelColor());
            loanLabelInfo.setLabelType(manualLabel.getLabelType());
            loanLabelInfo.setLabelLocation(manualLabel.getLabelLocation());
            loanLabelInfo.setLabelSort(manualLabel.getLabelSort());
            loanLabelInfo.setLabelPattern(manualLabel.getLabel());
            loanLabelInfo.setLabelClassification(manualLabel.getLabelClassification());
            loanLabelInfo.setCreateBy(SecurityUtils.getUsername());
            labelList.add(loanLabelInfo);
        }
        loanLabelInfoService.saveBatch(labelList);
        /**案件基础信息*/
        CaseContractInfo caseContractInfo = caseContractInfoService.getOne(Wrappers.<CaseContractInfo>query().lambda()
                .eq(CaseContractInfo::getApplyNo, applyNo));
        if (ObjectUtils.isNotEmpty(caseContractInfo)) {
            caseContractInfo.setLabelIdRepeat(strIds);
            caseContractInfoService.updateById(caseContractInfo);
        }
        return new IResponse<Boolean>().setMsg("新增标签库成功");
    }

    @PostMapping(value = "/queryAllLoanLabelList")
    @ApiOperation(value = "查詢所有信息数据")
    public IResponse<IPage<ManualLabel>> queryAllLoanLabelList(@PathVariable ManualLabelCondition manualLabel) {
        return IResponse.success(manualLabelService.page(new Page(manualLabel.getPageNumber(), manualLabel.getPageSize()), Wrappers.<ManualLabel>query().lambda()
                .like(StringUtils.isNotEmpty(manualLabel.getLabelName()), ManualLabel::getLabelName, manualLabel.getLabelName())
        ));
    }

    @PostMapping(value = "/queryAllLoanLabelByApplyNo/{applyNo}")
    @ApiOperation(value = "根据申请编号查询数据")
    public IResponse<List<LoanLabelInfo>> queryAllLoanLabelByApplyNo(@PathVariable String applyNo) {

        return IResponse.success(loanLabelInfoService.queryLoanLabelByApplyNo(applyNo));
    }

    @PostMapping(value = "/queryAllLoanLabelById/{id}")
    @ApiOperation(value = "根据申请编号差数据")
    public IResponse<LoanLabelInfo> queryAllLoanLabelById(@PathVariable String id) {

        return IResponse.success(loanLabelInfoService.getById(id));
    }

}